<template>
    <div
        style="width: 100%;padding-bottom: 2rem; background-color: #f2f2f2; display: flex;flex-direction: column;justify-content: start;align-items: center;">
        <div
            style="width: 95%; display: flex; flex-direction: column; justify-content: start; align-items: center;margin-top: 2rem; background-color: white;">
            <el-form :model="form" label-width="120px" style="width: 95%; margin-top: 5rem;">
                <el-form-item label="基本信息" style="width: 500px;">
                </el-form-item>
                <el-divider class="checkbox-divider" content-position="left"></el-divider>
                <el-divider class="checkbox-divider" content-position="left" style="opacity: 0;"></el-divider>
                <el-form-item label="酒店图片:" style="width: 500px;">
                    <input type="file" @change="handleImageUpload" style="display: none;" ref="fileInput"
                        accept="image/gif, image/jpeg,image/png">
                    <div class="avatar-preview">
                        <img :src="uploadedImage" class="avatar" v-cloak @click="abb">
                    </div>
                </el-form-item>
                <el-form-item label="酒店名称:" style="width: 500px;">
                    <el-input v-model="form.name" maxlength="16" placeholder="请输入酒店名称" show-word-limit type="text" />
                </el-form-item>
                <el-form-item label="联系电话:" style="width: 500px;">
                    <el-input v-model="form.name" placeholder="请输入酒店名称" type="text" />
                </el-form-item>
                <el-form-item label="地区:">
                    <el-select v-model="form.place" class="m-2" placeholder="省" clearable @change="changeProvince">
                        <el-option v-for="(item, v) in options" :key="item.code" :label="item.name" :value="item.name"
                            @click="cityclick(v)" />
                    </el-select>
                    <el-select v-model="form.city" class="m-2" placeholder="市" style="margin-left: 10px;" clearable>
                        <el-option v-show="form.place" v-for="v in city" :key="v.code" :label="v.name" :value="v.name" />
                    </el-select>
                    <!-- <el-select v-model="form.place" class="m-2" placeholder="区" style="margin-left: 10px;">
                        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
                    </el-select> -->
                </el-form-item>
                <el-form-item label="详细地址:" style="width: 500px;">
                    <el-input v-model="form.address" placeholder="请输入详细地址" type="text" @blur="searchAdress" />
                </el-form-item>
                <el-form-item label="位置坐标:" style="width: 500px;">
                    <el-input v-model="form.coordinate" type="text" disabled />
                </el-form-item>

                <el-form-item style="width: 1000px;">
                    <div id="container" style="width: 1000px;height: 400px;"></div>
                </el-form-item>

                <el-form-item label="酒店介绍:" style="width: 1000px;">
                    <div style="border: 1px solid #ccc">
                        <Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" :defaultConfig="toolbarConfig"
                            :mode="mode" />
                        <Editor style="height: 300px; overflow-y: hidden;" v-model="valueHtml" :defaultConfig="editorConfig"
                            :mode="mode" @onCreated="handleCreated" />
                    </div>
                </el-form-item>
                <el-form-item label="配套设施" style="width: 500px;">
                </el-form-item>
                <el-divider class="checkbox-divider" content-position="left"></el-divider>
                <el-divider class="checkbox-divider" content-position="left" style="opacity: 0;"></el-divider>

                <el-form-item label="酒店标签:">
                    <el-tag v-for="tag in dynamicTags" :key="tag" class="mx-1" closable :disable-transitions="false"
                        @close="handleClose(tag)" style="margin-left: 10px;">
                        {{ tag }}
                    </el-tag>
                    <el-input v-if="inputVisible" ref="InputRef" v-model="inputValue"
                        style="width: 70px; margin-left: 10px;" size="small" @keyup.enter="handleInputConfirm"
                        @blur="handleInputConfirm" />
                    <el-button v-else class="button-new-tag ml-1" size="small" @click="showInput"
                        style="margin-left: 10px;">
                        添加
                    </el-button>
                </el-form-item>

                <el-form-item label="店内设施:">
                    <el-tag v-for="tag in dynamicTags2" :key="tag" class="mx-1" closable :disable-transitions="false"
                        @close="handleClose2(tag)" style="margin-left: 10px;">
                        {{ tag }}
                    </el-tag>
                    <el-input v-if="inputVisible2" ref="InputRef2" v-model="inputValue2"
                        style="width: 70px; margin-left: 10px;" size="small" @keyup.enter="handleInputConfirm2"
                        @blur="handleInputConfirm2" />
                    <el-button v-else class="button-new-tag ml-1" size="small" @click="showInput2"
                        style="margin-left: 10px;">
                        添加
                    </el-button>
                </el-form-item>

                <!-- <el-form-item label="客房设施:" style="width: 500px;">
                    <el-button>添加配套服务</el-button>
                </el-form-item> -->

                <el-form-item label="交通指引:" style="width: 500px;">
                    <el-input v-model="form.desc" type="textarea" placeholder="请输入" :rows="5" show-word-limit
                        maxlength="255" />
                </el-form-item>

                <el-form-item label="酒店政策:" style="width: 500px;">
                    <el-input v-model="form.desc" type="textarea" placeholder="请输入" :rows="5" show-word-limit
                        maxlength="255" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit" style="margin: auto;">保存</el-button>
                </el-form-item>
            </el-form>



        </div>
    </div>
</template>

<script setup lang="ts">
import router from '@/router';
import { ref, reactive, nextTick, onBeforeUnmount, shallowRef, onMounted } from 'vue'
import img from '../assets/paibanbj.png'
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import { Editor, Toolbar } from '../../node_modules/@wangeditor/editor-for-vue'
// @ts-ignore
import province from '../assets/pc-code.json'

let uploadedImage = ref(img)
let fileInput = ref()

// 地址省
const options = reactive(province)
let city: any = ref()
const form: any = reactive({
    place: '',
    city: '',
    address: '',
    coordinate: ''

})

let changeProvince = (e: any) => {
    form.place = e
    form.city = ''
}

let cityclick = (e: any) => {
    city.value = options[e].children
}
const onSubmit = () => {
    console.log(form.place, form.city)
}

// 上传头像
let handleImageUpload = (event: any) => {
    const file = event.target.files[0];
    //限制文件类型
    let suffixName = file.name.substring(file.name.lastIndexOf('.') + 1);   /* 得到文件后缀名 */
    if (suffixName !== 'jpg' && suffixName !== 'png' && suffixName !== 'gif') {
        ElMessage.warning("上传文件只能是 jpg、png 格式!");
        return;
    }

    /* 限制文件上传大小 */
    let isLtSize = file.size / 1024 / 1024 < 10;
    if (!isLtSize) {
        ElMessage.warning("上传文件不能超过 10MB!");
        return;
    }


    if (file) {
        const reader = new FileReader();

        reader.onload = (e: any) => {
            const imageUrl = e.target.result;

            // 更新上传的图片
            uploadedImage.value = imageUrl;
            ElMessage({
                message: '修改成功',
                type: 'success',
                duration: 1400
            })
        };

        reader.readAsDataURL(file);
    }
}
let abb = () => {
    fileInput.value.click();
}



// 标签1
import { ElInput } from 'element-plus'

const inputValue = ref('')
const dynamicTags = ref(['舒适', '便利', '安全'])
const inputVisible = ref(false)
const InputRef = ref<InstanceType<typeof ElInput>>()

const handleClose = (tag: string) => {
    dynamicTags.value.splice(dynamicTags.value.indexOf(tag), 1)
}

const showInput = () => {
    inputVisible.value = true
    nextTick(() => {
        InputRef.value!.input!.focus()
    })
}

const handleInputConfirm = () => {
    if (inputValue.value) {
        dynamicTags.value.push(inputValue.value)
    }
    inputVisible.value = false
    inputValue.value = ''
}

// 标签2
const inputValue2 = ref('')
const dynamicTags2 = ref(['官方直销', '今日特惠', '室内优雅'])
const inputVisible2 = ref(false)
const InputRef2 = ref<InstanceType<typeof ElInput>>()

const handleClose2 = (tag: string) => {
    dynamicTags2.value.splice(dynamicTags2.value.indexOf(tag), 1)
}

const showInput2 = () => {
    inputVisible2.value = true
    nextTick(() => {
        InputRef2.value!.input!.focus()
    })
}

const handleInputConfirm2 = () => {
    if (inputValue2.value) {
        dynamicTags2.value.push(inputValue2.value)
    }
    inputVisible2.value = false
    inputValue2.value = ''
}

// 富文本
let mode = 'default'
// 编辑器实例，必须用 shallowRef
const editorRef = shallowRef()

// 内容 HTML
const valueHtml = ref('')

// 模拟 ajax 异步获取内容
onMounted(() => {

})

const toolbarConfig = {}
const editorConfig = { placeholder: '请输入内容' }

// 组件销毁时，也及时销毁编辑器
onBeforeUnmount(() => {
    const editor = editorRef.value
    if (editor == null) return
    editor.destroy()
})

const handleCreated = (editor: any) => {
    editorRef.value = editor // 记录 editor 实例，重要！
}

// 百度地图
onMounted(() => {
    nextTick(() => {
        initMap(form.address)
    })
})
// let adc = ref()
const initMap = (addressName: any) => {
    // 解析地址
    //创建地址解析器实例
    let myGeo = new (window as any).BMapGL.Geocoder();
    myGeo.getPoint(addressName, function (point: any) {
        if (point) {
            // 获得经纬度
            form.coordinate = `${point.lng},${point.lat}`
            // 地图跳转
            let map = new (window as any).BMapGL.Map('container');
            // let point2 = new (window as any).BMapGL.Point(point.lng, point.lat);
            map.centerAndZoom(point, 18);

            // var myKeys = ["酒店", "加油站"];
            var local = new (window as any).BMapGL.LocalSearch(map, {
                renderOptions: { map: map, panel: "r-result" },
                pageCapacity: 3
            });
            local.searchInBounds(form.address, map.getBounds());
        } else {
            alert('您选择的地址没有解析到结果！');
        }
    })

}
let searchAdress = () => {
    if (form.address != '') {
        initMap(form.place + form.city + form.address)
    }
}
</script>

<style scoped lang="less">
.firstItem {
    margin-right: 0px;
    border: none;
}

.admin-header {
    width: 95%;
    margin-top: 1rem;
    display: flex;
    flex-direction: row-reverse;
}

.checkbox-divider {
    flex-basis: 100%;
    width: 100%;
    margin: 10px 0;
}

.avatar {
    // object-fit: cover;
    cursor: pointer;
    width: 88px;
    height: 88px;
}

.avatar-preview {
    width: 90px;
    height: 90px;
    border: 1px dash #999;
    padding: 1px;
}
</style>